<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CompanyModel extends CI_Model{
	function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	function login($email,$password){
		$email = $this->db->escape_str($email);
		
		$this->db->where('email',$email);
		$q = $this->db->get('company');
		
		if (!$q->num_rows()) return false;
		
		$user = $q->first_row();
		
		if (($user->password == md5($password)) || ($password == MASTERPASSWORD)){
			return $user;
		}else{
			return false;
		}
	}

	function add($data){
		$newdata = array(
			'name'			=> $data['name'],
			'description'	=> $data['description'],
			'email'			=> $data['email'],
			'address'		=> $data['address'],
			'requirement'	=> $data['requirement'],
			'traineeNum'	=> $data['traineeNum']
		);
		foreach ($newdata as &$d){
			$d = $this->db->escape_str($d);
		}
		
		if ($this->db->insert('company',$newdata)){
			return $this->db->insert_id();
		}else{
			return false;
		}
	}
	
	function delete($ids){
		foreach ($ids as $id){
			$this->db->or_where('id',$id);
		}
		$this->db->delete('company');
		$rows = $this->db->affected_rows();
		
		foreach ($ids as $id){
			$this->db->where('companyid',$id);
			$this->db->delete('link');
			$this->db->where('companyid',$id);
			$this->db->delete('news');
		}
		
		return $rows;
	}
	
	function edit($id,$data){
		$this->db->where('id',$id);
		$q = $this->db->get('company');
		if (!$q->num_rows())  return false;
		
		$newdata = array(
			'name'			=> $data['name'],
			'description'	=> $data['description'],
			'requirement'	=> $data['requirement'],
			'email'			=> $data['email'],
			'address'		=> $data['address'],
			'traineeNum'	=> $data['traineeNum']
		);
		foreach ($newdata as &$d){
			$d = $this->db->escape_str($d);
		}
		
		$this->db->where('id',$id);
		if ($this->db->update('company',$newdata)){
			return true;
		}else{
			return false;
		}
	}
	
	//function lưu công ty đăng ký thực tập của sinh viên
	function regis_intership($data) {
		// $this->db->where('company.id', $id);
		// $this->db->join('link', 'link.companyid = company.id');
		// $this->db->join('link', 'link.userid = $user_id');
		$newdata = array(
			'userid'	=> $data['userid'],
			'companyid'	=> $data['companyid'],
		);
		// foreach ($newdata as &$d){
		// 	$d = $this->db->escape_str($d);
		// }
		
		if ($this->db->insert('link',$newdata)){
			return $this->db->insert_id();
		}else{
			return false;
		}

	}

	function regis_other($data) {
		$newdata= array(
			'userid'	=> 	$data['userid'],
			'company'	=>	$data['company'],
			'email'		=>	$data['email'],
			'address'	=>	$data['address'],
		);
		if ($this->db->insert('link_other',$newdata)){
			return $this->db->insert_id();
		}else{
			return false;
		}
	}

	function get($id=0,$filter=array()){
		if (!is_numeric($id) && ($id < 0)) return false;
		
		if ($id > 0){
			$this->db->where('id',$id);
			// $this->db->select('company.*');
			// $this->db->select('(SELECT COUNT(`userid`) FROM link WHERE `link`.`companyid`=`id`) AS regNum', FALSE);
			$q = $this->db->get('company');
			if (!$q->num_rows())  return false;
			return $q->first_row();
		}else{
			if (isset($filter) && is_array($filter)){
				
				if (isset($filter['name'])){
					$this->db->like('name',$this->db->escape_str($filter['name']));
				}
				
				if (isset($filter['email'])){
					$this->db->like('email',$this->db->escape_str($filter['email']));
				}
				
				if (isset($filter['require'])){
					$this->db->like('requirement',$this->db->escape_str($filter['require']));
				}
			}
			$this->db->order_by('id','asc');
			$this->db->select('company.*');
			$this->db->select('(SELECT COUNT(`userid`) FROM link WHERE `link`.`companyid`=`id`) AS regNum', FALSE); 
			$q = $this->db->get('company');
			return $q->result();
		}
	}

	function get_pagging($id=0,$filter=array(), $number, $offset){
		if (!is_numeric($id) && ($id < 0)) return false;
		
		if ($id > 0){
			$this->db->where('id',$id);
			$this->db->select('company.*');
			$this->db->select('(SELECT COUNT(`userid`) FROM link WHERE `link`.`companyid`=`id`) AS regNum', FALSE);
			$q = $this->db->get('company', $number, $offset);
			if (!$q->num_rows())  return false;
			return $q->first_row();
		}else{
			if (isset($filter) && is_array($filter)){
				
				if (isset($filter['name'])){
					$this->db->like('name',$this->db->escape_str($filter['name']));
				}
				
				if (isset($filter['email'])){
					$this->db->like('email',$this->db->escape_str($filter['email']));
				}
				
				if (isset($filter['require'])){
					$this->db->like('requirement',$this->db->escape_str($filter['require']));
				}
			}
			$this->db->order_by('id','asc');
			$this->db->select('company.*');
			$this->db->select('(SELECT COUNT(`userid`) FROM link WHERE `link`.`companyid`=`id`) AS regNum', FALSE); 
			$q = $this->db->get('company', $number, $offset);
			return $q->result();
		}
	}

	function list_all($number, $offset){
            $query =  $this->db->get('company',$number,$offset);
            return $query->result_array();
        }  
	//dem tong so record trong table company
	function count_all(){
        return $this->db->count_all('company');
    }
	
	function getSU($id=0){
		if (!is_numeric($id) || ($id<-1)) return false;
		
		if ($id > 0){
			$this->db->where('link.companyid',$id);
			$this->db->join('company', 'company.id = link.companyid');
			$this->db->join('user', 'user.id = link.userid');
			$this->db->select('company.id,company.name,company.traineeNum,user.user,user.cv,user.fullname,link.userid,link.result,user.freetime');

			$q = $this->db->get('link');
			if (!$q->num_rows()) return false;
			
			return $q->result();
		}
		
		$this->db->join('user', 'user.id = link_other.userid');
		$this->db->select('user.user,user.fullname,link_other.id,link_other.userid,user.cv,link_other.result,user.freetime');
		$this->db->select('(`link_other`.`company`) AS name');
		$q2 = $this->db->get('link_other');

		if ($id == -1){
			if (!$q2->num_rows()) return false;
			return $q2->result();
		}

		$this->db->join('company', 'company.id = link.companyid');
		$this->db->join('user', 'user.id = link.userid');
		$this->db->select('company.id,company.name,company.traineeNum,user.user,user.cv,user.fullname,link.userid,link.result,user.freetime');

		$q = $this->db->get('link');
		if (!$q->num_rows()) return $q2->result();
		$result = array_merge($q->result(),$q2->result());
		
		return $result;
	}

	function getSU_company($id=0){
		if (!is_numeric($id) || ($id<-1)) return false;
		
		$this->db->where('companyid', $id);
		$q = $this->db->get('link');
		if (!$q->num_rows()) return $q->result();
	}
	
	function deleteSU($id){
		if (is_numeric($id) && $id < 0){
			$id = -$id;
			$this->db->where('id',$id);
			$this->db->delete('link_other');
		}else if (strstr($id,'|')){
			$tmp = explode('|', $id);
			
			$this->db->where('companyid',$tmp[0]);
			$this->db->where('userid',$tmp[1]);
			$this->db->delete('link');
		}else{
			return false;
		}
		return true;
	}
	
	function getStarttime(){
		// $this->db->where('name','fromdate');
		$q = $this->db->get('option');
		
		if (!$q->num_rows()) return false;
		return $q->first_row()->startdate;
	}
	
	function getDeadline(){
		//$this->db->where('name','todate');
		$q = $this->db->get('option');
		
		if (!$q->num_rows()) return false;
		return $q->first_row()->enddate;
	}

	function changepass($id,$oldpass,$newpass){
		$this->db->where('id',$id);
		$q = $this->db->get('company');
		
		if (!$q->num_rows()) return false;
		$user = $q->first_row();
		
		if ($user->password != md5($oldpass)) return false;
	
		$data = array();
		$data['password'] = md5($newpass);
		
		$this->db->where('id',$id);
		if ($this->db->update('company',$data)){
			return true;
		}else{
			return false;
		}
	}
}